// import Vue from 'vue'
// import VueRouter from 'vue-router'
// Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home'),
    meta: {
      title: 'MindFall-首页'
    }
  },
  {
    path: '/mobile404',
    name: "Mobile_404",
    component: () => import('../views/Mobile_404'),
    meta: {
      title: 'MindFall-请使用PC访问'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Verify'),
    meta: {
      title: 'MindFall-登录'
    }
  },

  {
    path: '/creative404',
    name: "Creative_404",
    component: () => import('../views/Creative_404'),
    meta: {
      title: '创意 NotFound'
    }
  },
  {
    path: '/solution404',
    name: "Solution_404",
    component: () => import('../views/Solution_404'),
    meta: {
      title: '解决方案 NotFound'
    }
  },
  {
    path: '/user404',
    name: "User_404",
    component: () => import('../views/User_404'),
    meta: {
      title: '用户 NotFound'
    }
  },

  {
    path: '/idea_submission',
    name: 'Idea_Submission',
    component: () => import('../views/Idea_Submission'),
    meta: {
      title: 'MindFall-创意提交'
    },
  },

  {
    path: '/intelligent_guidance',
    component: () => import('../views/About'),
    redirect: '/intelligent_guidance/info',

    children:[
      {
        path: 'info',
        name: 'guidance_info',
        component: () => import('../views/Guidance_Info'),
        // 页面标题位置修改 2022/3/25 cjb
        meta: {
          title: 'MindFall-创新引导',
        },
      },
      {
        path: 'questions',
        name: 'guidance_questions',
        component: () => import('../views/Guidance_Questions'),
        // 页面标题位置修改 2022/3/25 cjb
        meta: {
          title: 'MindFall-创新引导',
          // keepAlive: true
        },
      },
      // 模型图绘制页面
      {
        path: 'model',
        name: 'guidance_model',
        component: () => import('../views/Guidance_Model'),
        meta: {
          title: 'MindFall-创新引导',
          // keepAlive: true
        },
      },
      {
        path: 'retrieval',
        name: 'guidance_retrieval',
        component: () => import('../views/Guidance_Retrieval'),
        meta: {
          title: 'MindFall-创新引导',
          // keepAlive: true
        },
      },
      {
        path: 'solution_edit',
        name: 'solution_edit',
        component: () => import('../views/Solution_Edit'),
        meta: {
          title: '解决方案编辑',
          // keepAlive: true
        }
      }
      ]
  },

  {
    path: '/personal',
    component: () => import('../views/Personal'),
    redirect: '/personal/user',
    children: [
      {
        path: 'message',
        name: 'Message',
        component: () => import('../views/Message'),
        meta: {
          title: 'MindFall-通知中心'
        }
      },
      {
        path: 'solution',
        name: 'Solutions',
        component: () => import('../views/Solution'),
        meta: {
          title: 'MindFall-我的解决方案仓库'
        }
      },
      {
        path: 'note',
        name: 'Note',
        component: () => import('../views/Note'),
        meta: {
          title: 'MindFall-我的专利笔记'
        }
      },
      {
        path: 'creativity',
        name: 'Creativity',
        component: () => import('../views/Creativity'),
        meta: {
          title: 'MindFall-我的创意仓库'
        }
      },
      {
        path: 'guide',
        name: 'Guide',
        component: () => import('../views/Guide'),
        meta: {
          title: 'MindFall-我的创新引导仓库'
        }
      },
      {
        path: 'user',
        name: 'Person',
        component: () => import('../views/Person'),
        meta: {
          title: '个人主页'
        },
      },
      {
        path: 'edit',
        name: 'Info_Edit',
        component: () => import('../views/Personal_Info_Edit'),
        meta: {
          title: '编辑个人资料'
        },
      },
    ]
  },
  {
    path: '/userInfo',
    name: 'User_Detail',
    component: () => import('../views/User_Detail'),
    meta: {
      title: '用户主页'
    }
  },

  {
    path: '/creativity_community',
    name: 'Creativity_Community',
    component: () => import('../views/Creativity_Community'),
    meta: {
      title: 'MindFall-创意社区'
    }
  },
  {
    path: '/creativeInfo',
    name: "Creativity_Detail",
    component: () => import('../views/Creativity_Detail'),
    meta: {
      title: '创意详情'
    }
  },

  {
    path: '/solution_community',
    name: 'Solution_Community',
    component: () => import('../views/Solution_Community'),
    meta: {
      title: 'MindFall-解决方案社区'
    }
  },
  {
    path: '/solutionInfo',
    name: "Solution_Detail",
    component: () => import('../views/Solution_Detail'),
    meta: {
      title: '方案详情'
    }
  },

  {
    path: '/manager',
    component: () => import('../views/admin/Manager'),
    redirect: '/manager/manage_home',
    meta: {
      title: 'MindFall-管理系统'
    },
    children:[
      //用户管理
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/admin/User'),
      },
      //主页
      {
        path: 'manage_home',
        name: 'manage_home',
        component: () => import('../views/admin/Manage_Home'),
      },
      //角色管理
      {
        path: 'role',
        name: 'role',
        component: () => import('../views/admin/Role'),
      },
      //菜单管理
      {
        path: 'menu',
        name: 'menu',
        component: () => import('../views/admin/Menu'),
      },
      //解决方案管理
      {
        path: 'solution',
        name: 'Solution',
        component: () => import('../views/admin/Solution')
      },
      //创意管理
      {
        path: 'creative',
        name: 'Creative',
        component: () => import('../views/admin/Creative')
      },
      //创意评论管理
      {
        path: 'creative_comment',
        name: 'Creative_Comment',
        component: () => import('../views/admin/Creative_Comment')
      },
      //方案评论管理
      {
        path: 'solution_comment',
        name: 'Solution_Comment',
        component: () => import('../views/admin/Solution_Comment')
      },
      //解决方案审核
      {
        path: 'solution_check',
        name: 'Solution_Check',
        component: () => import('../views/admin/Solution_Check')
      },
      //创意审核
      {
        path: 'creative_check',
        name: 'Creative_Check',
        component: () => import('../views/admin/Creative_Check')
      },
      //头像审核
      {
        path: 'avatar_check',
        name: 'Avatar_Check',
        component: () => import('../views/admin/Avatar_Check')
      },
    ]
  },

]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // 打开新页面定位到开始位置
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0}
  }
})

let userLimitPagePath = [
  '/idea_submission',

  '/intelligent_guidance', '/intelligent_guidance/info', '/intelligent_guidance/questions',
  '/intelligent_guidance/model', '/intelligent_guidance/retrieval', '/intelligent_guidance/solution_edit',

  '/userInfo', '/creativeInfo', '/solutionInfo',

  '/creativity_community', '/solution_community',

  '/personal', '/personal/user', '/personal/note', '/personal/guide', '/personal/creativity', '/personal/solution', '/personal/message'
]

let guidePagePath = [
  '/intelligent_guidance/questions', '/intelligent_guidance/model',
  '/intelligent_guidance/retrieval', '/intelligent_guidance/solution_edit',
]

let adminPagePath = [
  '/manager', '/manager/manage_home', '/manager/solution_comment', '/manager/creative_comment',
  '/manager/user', '/manager/role', '/manager/menu', '/manager/solution', '/manager/creative',
  '/manager/solution_check', '/manager/creative_check', '/manager/avatar_check'
]



router.beforeEach((to, from, next) => {
  if ('/login' === to.path) {
    if (localStorage.getItem('mindfall-ticket') || localStorage.getItem("_u")) {
      next({path: "/"})
    }
    else {next()}
  }
  else {next()}

  if (userLimitPagePath.includes(to.path)) {
    let userStr = localStorage.getItem("_u")
    if (!userStr || userStr !== 'user') {
      // 跳转到登录页面
      next({path: '/login'})
    }
    else {next()}
  }
  else if (adminPagePath.includes(to.path)) {
    let adminStr = localStorage.getItem("_u")
    if (!adminStr || adminStr !== 'Porn6-413-Admin'){
      next({path: '/login'})
    }
    else {next()}
  }
  else {next()}

  if (guidePagePath.includes(to.path)) {
    let guideStr = sessionStorage.getItem("guideId")
    if (!guideStr || guideStr === '') {
      // 跳转到登录页面
      next({path: "/personal/guide"})
    }
    else {next()}
  }
  else {next()}


})

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


export default router
